package com.yanggeng.demo.beginnerguidedemo.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yanggeng.demo.beginnerguidedemo.activity.DrawerLayoutActivity;
import com.yanggeng.demo.beginnerguidedemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanggeng on 2017/7/6.
 */

public class ActivityFragment extends Fragment {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    List<Fragment> fragmentList;
    String[] titles = {"i车活动", "同城活动", "我的活动"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity, null);
        toolbar = (Toolbar) view.findViewById(R.id.activity_toolbar);
        tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        viewPager = (ViewPager) view.findViewById(R.id.vp_activity);
        initViewPager();

        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setupWithViewPager(viewPager);

        toolbar.setNavigationIcon(R.mipmap.c_navbar_menu);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((DrawerLayoutActivity) getActivity()).getDrawerLayout().openDrawer(GravityCompat.START);
            }
        });

        return view;
    }

    private void initViewPager() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new TestFragment());
        fragmentList.add(new TestFragment());
        fragmentList.add(new TestFragment());
        viewPager.setAdapter(new ActivityAdapter(getChildFragmentManager()));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    class ActivityAdapter extends FragmentPagerAdapter {

        public ActivityAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
